/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.form;

import model.Storage;
import domen.Korisnik;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import model.StorageBroker;

/**
 *
 * @author student1
 */
public class FKorisnik extends javax.swing.JDialog {

    /**
     * Creates new form FKorisnik
     */
    public FKorisnik(java.awt.Frame parent, boolean modal) {
        initComponents();
        srediFormu();
        jbtnSacuvaj.setVisible(false);
        jbtnObrisi.setVisible(false);

    }
    
    public FKorisnik(java.awt.Frame parent, boolean modal,Korisnik korisnik){
    
        initComponents();
        srediFormu();
        sb = new StorageBroker(new Korisnik());
        jbtnSacuvaj.setVisible(false);

        odabraniKorisnik = korisnik;

        jpImeKorisnika.vratiKomponentuTexta().setText(odabraniKorisnik.getIme());
        jpPrezime.vratiKomponentuTexta().setText(odabraniKorisnik.getPrezime());
        jpEmail.vratiKomponentuTexta().setText(odabraniKorisnik.getEmail());
        jpKorisnickoIme.vratiKomponentuTexta().setText(odabraniKorisnik.getKorisickoIme());
        jpKorisnickaSifra.vratiKomponentuTexta().setText(odabraniKorisnik.getKorisnickaSifra());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpImeKorisnika = new ui.components.JPanelTextField();
        jpPrezime = new ui.components.JPanelTextField();
        jpEmail = new ui.components.JPanelTextField();
        jpKorisnickoIme = new ui.components.JPanelTextField();
        jbtnSacuvaj = new javax.swing.JButton();
        jbtnIzmeni = new javax.swing.JButton();
        jbtnObrisi = new javax.swing.JButton();
        jpKorisnickaSifra = new ui.components.JPanelPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jbtnSacuvaj.setText("Sacuvaj");
        jbtnSacuvaj.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSacuvajActionPerformed(evt);
            }
        });

        jbtnIzmeni.setText("Izmeni");
        jbtnIzmeni.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnIzmeniActionPerformed(evt);
            }
        });

        jbtnObrisi.setText("Obrisi");
        jbtnObrisi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnObrisiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jpImeKorisnika, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jpPrezime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jbtnSacuvaj, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jbtnIzmeni, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jbtnObrisi, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jpKorisnickaSifra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jpEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jpKorisnickoIme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jbtnSacuvaj)
                .addGap(19, 19, 19)
                .addComponent(jbtnIzmeni)
                .addGap(18, 18, 18)
                .addComponent(jbtnObrisi)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpImeKorisnika, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpPrezime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpKorisnickoIme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpKorisnickaSifra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnSacuvajActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSacuvajActionPerformed

        uzmiPodatkeSaForme();

        if (validacijaKorisnika()) {

            Korisnik korisnik = new Korisnik();
            korisnik.setIme(ime);
            korisnik.setPrezime(prezime);
            korisnik.setEmail(email);
            korisnik.setKorisickoIme(korisnickiIme);
            korisnik.setKorisnickaSifra(korisnickaSifra);

            StorageBroker sb = new StorageBroker(korisnik);
            sb.ubaciEntitetUMapu();

            JOptionPane.showMessageDialog(this, "Korisnik je uspesno sacuvan");

            resetujPoljaNaFormi();
        }

    }//GEN-LAST:event_jbtnSacuvajActionPerformed

    private void jbtnIzmeniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnIzmeniActionPerformed
        uzmiPodatkeSaForme();

        if (validacijaKorisnika()) {
            odabraniKorisnik.setIme(ime);
            odabraniKorisnik.setPrezime(prezime);
            odabraniKorisnik.setEmail(email);
            odabraniKorisnik.setKorisickoIme(korisnickiIme);
            odabraniKorisnik.setKorisnickaSifra(korisnickaSifra);

            sb.izmeniEntitet(odabraniKorisnik.getKorisickoIme(), odabraniKorisnik);
            
            JOptionPane.showMessageDialog(this, "Podaci korisnika su uspesno izmenjeni.");
            this.setVisible(false);
            this.dispose();

          
    }//GEN-LAST:event_jbtnIzmeniActionPerformed
     }
    private void jbtnObrisiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnObrisiActionPerformed
        sb.izbrisiEntitet(odabraniKorisnik.getKorisickoIme());
       JOptionPane.showMessageDialog(this, "Korisnik je uspesno izbrisan.");
    }//GEN-LAST:event_jbtnObrisiActionPerformed

     /**
      * @param args the command line arguments
      */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jbtnIzmeni;
    private javax.swing.JButton jbtnObrisi;
    private javax.swing.JButton jbtnSacuvaj;
    private ui.components.JPanelTextField jpEmail;
    private ui.components.JPanelTextField jpImeKorisnika;
    private ui.components.JPanelPasswordField jpKorisnickaSifra;
    private ui.components.JPanelTextField jpKorisnickoIme;
    private ui.components.JPanelTextField jpPrezime;
    // End of variables declaration//GEN-END:variables

    String ime;
    String prezime;
    String korisnickiIme;
    String email;
    String korisnickaSifra;
    Korisnik odabraniKorisnik;
    StorageBroker sb;

    private void srediFormu() {
        jpImeKorisnika.vratiKomponentuZaPrikazTeksta().setText("Ime:");
        jpImeKorisnika.vratiKomponentuZaPrikazGreske().setText(" ");

        jpPrezime.vratiKomponentuZaPrikazTeksta().setText("Prezime:");
        jpPrezime.vratiKomponentuZaPrikazGreske().setText(" ");

        jpEmail.vratiKomponentuZaPrikazTeksta().setText("Email:");
        jpEmail.vratiKomponentuZaPrikazGreske().setText(" ");

        jpKorisnickoIme.vratiKomponentuZaPrikazTeksta().setText("Korisnicko ime:");
        jpKorisnickoIme.vratiKomponentuZaPrikazGreske().setText(" ");

        jpKorisnickaSifra.vratiKomponentuZaPrikazGreske().setText(" ");

        resetujPoljaNaFormi();
    }

    private boolean validacijaKorisnika() {

        boolean validacija = true;

        try {
            if (ime.isEmpty()) {
                jpImeKorisnika.vratiKomponentuZaPrikazGreske().setText("Polje ime mora biti uneto!");
                //throw new Exception("Validacija nije uspela");
                validacija = false;
            } else {
                jpImeKorisnika.vratiKomponentuZaPrikazGreske().setText(" ");
            }

            if (prezime.isEmpty()) {
                jpPrezime.vratiKomponentuZaPrikazGreske().setText("Polje prezime mora biti "
                        + "uneto!");
                validacija = false;
            } else {
                jpPrezime.vratiKomponentuZaPrikazGreske().setText(" ");
            }

            if (korisnickiIme.isEmpty()) {
                jpKorisnickoIme.vratiKomponentuZaPrikazGreske().setText("Polje korisnicko ime "
                        + "mora biti uneto!");
                validacija = false;
            } else {
                jpKorisnickoIme.vratiKomponentuZaPrikazGreske().setText(" ");
            }

            if (email.isEmpty()) {
                jpEmail.vratiKomponentuZaPrikazGreske().setText("Polje email mora biti uneto!");
                validacija = false;
            } else {
                jpEmail.vratiKomponentuZaPrikazGreske().setText(" ");
            }

            if (korisnickaSifra.isEmpty()) {
                jpKorisnickaSifra.vratiKomponentuZaPrikazGreske().setText("Polje korisnicka sifra "
                        + "mora biti uneto!");
                validacija = false;
            } else {
                jpKorisnickaSifra.vratiKomponentuZaPrikazGreske().setText(" ");
            }

            return validacija;
        } catch (Exception e) {
            System.out.println("Validacija nije uspela!");
            JOptionPane.showMessageDialog(this, e.getMessage());
        }
        return false;
    }

    private void uzmiPodatkeSaForme() {
        ime = jpImeKorisnika.vratiKomponentuTexta().getText().trim();
        prezime = jpPrezime.vratiKomponentuTexta().getText().trim();
        korisnickiIme = jpKorisnickoIme.vratiKomponentuTexta().getText().trim();
        email = jpEmail.vratiKomponentuTexta().getText().trim();

        korisnickaSifra = jpKorisnickaSifra.vratiKomponentuTexta().getPassword().toString();
    }

    private void resetujPoljaNaFormi() {
        jpImeKorisnika.vratiKomponentuTexta().setText("");
        jpPrezime.vratiKomponentuTexta().setText("");
        jpEmail.vratiKomponentuTexta().setText("");
        jpKorisnickoIme.vratiKomponentuTexta().setText("");
        jpKorisnickaSifra.vratiKomponentuTexta().setText("");
    }

}
